//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    url: app.globalData.url,
    imgPath: app.globalData.url+"/file/showImage/",
    imgUrls: [],
    msgList:[], 
    menu:[],
    policy:[],
    dongtai: []
  },
  //事件处理函数
  onLoad: function () {
    console.log("开始获取数据");
    wx.showLoading({
      title: '加载中',
    })
    this.onPullDownRefresh();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    var that = this;
    console.log("获取数据中......");
    wx.request({
      url: app.globalData.url + '/index', //仅为示例，并非真实的接口地址
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log("数据获取成功");
        console.log(res);
        console.log("数据渲染中");
        that.setData({
          // 轮播图
          imgUrls: res.data.result.baners[0],
          // 菜单
          menu: res.data.result.menu[0],
          // 公告
          msgList: res.data.result.announcement[0],
          // 最新政策
          policy: res.data.result.policy[0],
          dongtai: res.data.result.work_dynamic[0]
        })
        console.log("数据成功");
        wx.hideLoading()
      }
    });
  },
    addPraise: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
      var dongtai = this.data.dongtai;
    wx.request({
      header: app.header(),
      url: app.globalData.url + '/praise/add/' + id,
      success: function (res) {
        //实际点赞发送
        if (res.data.status == "success") {
          dongtai[index].totalPraise++;
          that.setData({
            dongtai: dongtai
          });
        } else if (res.data.status == "no_user") {
          wx.showModal({
            title: '提示',
            content: "请登录后参加考试",
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else {
                console.log('用户点击取消')
              }
            }
          });
        }
      },
    })
  },

})
